package com.alpaca.recommend.action;

import com.alpaca.recommend.service.URecommendService;
import com.alpaca.recommend.util.Jackson2Util;
import com.alpaca.recommend.util.Paged;
import com.alpaca.recommend.util.Response;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.spring.Inject;
import com.jfinal.plugin.spring.IocInterceptor;

/**
 * Created by wangj on 2015/2/10.
 */
@Before(IocInterceptor. class)
public class RecommendController extends Controller{

    @Inject.BY_TYPE
    URecommendService uRecommendService;

    public void get(){
        String channelCode = getPara("channelCode");
        String userId = getPara("userId");
        Integer pageNo = getParaToInt("pageNo");
        Integer pageSize = getParaToInt("pageSize");

        if (userId == null) {
            userId = "0";
        }
        if (channelCode == null) {
            renderText(Jackson2Util.toJson(Response.getFailedResponse("频道号不存在")));
            return;
        }

        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        Paged<Integer> itemIds = uRecommendService.findByPage(channelCode, userId, pageNo, pageSize);
        if(itemIds.getTotalHit()==0){
            itemIds = uRecommendService.findByPage(channelCode, "0", pageNo, pageSize);
        }
        renderText(Jackson2Util.toJson(itemIds));
    }

    public void view(){
        String channelCode = getPara("channelCode");
        String userId = getPara("userId");
        String itemId = getPara("itemId");
        String tags = getPara("tags");//TODO:处理传入特定TAG
        if (channelCode == null) {
            renderText(Jackson2Util.toJson(Response.getFailedResponse("频道号不存在")));
            return;
        }
        if (userId == null){
            renderText(Jackson2Util.toJson(Response.getFailedResponse("userId不能为空")));
            return;
        }
        if (itemId == null){
            renderText(Jackson2Util.toJson(Response.getFailedResponse("ItemId不能为空")));
            return;
        }
        boolean success = uRecommendService.add(channelCode, userId,itemId);
        renderText(Jackson2Util.toJson(Response.getSuccessResponse()));
    }
}
